"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.trTRIntl = exports.srRSIntl = exports.ruRUIntl = exports.ptBRIntl = exports.plPLIntl = exports.msMYIntl = exports.mnMNIntl = exports.koKRIntl = exports.jaJPIntl = exports.itITIntl = exports.intlMapKeys = exports.intlMap = exports.idIDIntl = exports.hrHRIntl = exports.frFRIntl = exports.faIRIntl = exports.esESIntl = exports.enUSIntl = exports.enGBIntl = exports.default = exports.deDEIntl = exports.createIntl = exports.caESIntl = exports.arEGIntl = exports.ProProvider = exports.ConfigProviderWrap = exports.ConfigProvider = exports.ConfigConsumer = void 0;
exports.useIntl = useIntl;
exports.zhTWIntl = exports.zhCNIntl = exports.viVNIntl = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));

var _jsxRuntime = require("react/jsx-runtime");

var _antdV = require("antd-v4");

var _zh_CN = _interopRequireDefault(require("antd-v4/lib/locale/zh_CN"));

var _react = _interopRequireWildcard(require("react"));

var _swr = require("swr");

var _ar_EG = _interopRequireDefault(require("./locale/ar_EG"));

var _ca_ES = _interopRequireDefault(require("./locale/ca_ES"));

var _de_DE = _interopRequireDefault(require("./locale/de_DE"));

var _en_GB = _interopRequireDefault(require("./locale/en_GB"));

var _en_US = _interopRequireDefault(require("./locale/en_US"));

var _es_ES = _interopRequireDefault(require("./locale/es_ES"));

var _fa_IR = _interopRequireDefault(require("./locale/fa_IR"));

var _fr_FR = _interopRequireDefault(require("./locale/fr_FR"));

var _hr_HR = _interopRequireDefault(require("./locale/hr_HR"));

var _id_ID = _interopRequireDefault(require("./locale/id_ID"));

var _it_IT = _interopRequireDefault(require("./locale/it_IT"));

var _ja_JP = _interopRequireDefault(require("./locale/ja_JP"));

var _ko_KR = _interopRequireDefault(require("./locale/ko_KR"));

var _mn_MN = _interopRequireDefault(require("./locale/mn_MN"));

var _ms_MY = _interopRequireDefault(require("./locale/ms_MY"));

var _pl_PL = _interopRequireDefault(require("./locale/pl_PL"));

var _pt_BR = _interopRequireDefault(require("./locale/pt_BR"));

var _ru_RU = _interopRequireDefault(require("./locale/ru_RU"));

var _sr_RS = _interopRequireDefault(require("./locale/sr_RS"));

var _tr_TR = _interopRequireDefault(require("./locale/tr_TR"));

var _vi_VN = _interopRequireDefault(require("./locale/vi_VN"));

var _zh_CN2 = _interopRequireDefault(require("./locale/zh_CN"));

var _zh_TW = _interopRequireDefault(require("./locale/zh_TW"));

function get(source, path, defaultValue) {
  // a[3].b -> a.3.b
  var paths = path.replace(/\[(\d+)\]/g, '.$1').split('.');
  var result = source;
  var message = defaultValue; // eslint-disable-next-line no-restricted-syntax

  var _iterator = (0, _createForOfIteratorHelper2.default)(paths),
      _step;

  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var p = _step.value;
      message = Object(result)[p];
      result = Object(result)[p];

      if (message === undefined) {
        return defaultValue;
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }

  return message;
}
/**
 * 创建一个操作函数
 *
 * @param locale
 * @param localeMap
 */


var createIntl = function createIntl(locale, localeMap) {
  return {
    getMessage: function getMessage(id, defaultMessage) {
      return get(localeMap, id, defaultMessage) || defaultMessage;
    },
    locale: locale
  };
};

exports.createIntl = createIntl;
var mnMNIntl = createIntl('mn_MN', _mn_MN.default);
exports.mnMNIntl = mnMNIntl;
var arEGIntl = createIntl('ar_EG', _ar_EG.default);
exports.arEGIntl = arEGIntl;
var zhCNIntl = createIntl('zh_CN', _zh_CN2.default);
exports.zhCNIntl = zhCNIntl;
var enUSIntl = createIntl('en_US', _en_US.default);
exports.enUSIntl = enUSIntl;
var enGBIntl = createIntl('en_GB', _en_GB.default);
exports.enGBIntl = enGBIntl;
var viVNIntl = createIntl('vi_VN', _vi_VN.default);
exports.viVNIntl = viVNIntl;
var itITIntl = createIntl('it_IT', _it_IT.default);
exports.itITIntl = itITIntl;
var jaJPIntl = createIntl('ja_JP', _ja_JP.default);
exports.jaJPIntl = jaJPIntl;
var esESIntl = createIntl('es_ES', _es_ES.default);
exports.esESIntl = esESIntl;
var caESIntl = createIntl('ca_ES', _ca_ES.default);
exports.caESIntl = caESIntl;
var ruRUIntl = createIntl('ru_RU', _ru_RU.default);
exports.ruRUIntl = ruRUIntl;
var srRSIntl = createIntl('sr_RS', _sr_RS.default);
exports.srRSIntl = srRSIntl;
var msMYIntl = createIntl('ms_MY', _ms_MY.default);
exports.msMYIntl = msMYIntl;
var zhTWIntl = createIntl('zh_TW', _zh_TW.default);
exports.zhTWIntl = zhTWIntl;
var frFRIntl = createIntl('fr_FR', _fr_FR.default);
exports.frFRIntl = frFRIntl;
var ptBRIntl = createIntl('pt_BR', _pt_BR.default);
exports.ptBRIntl = ptBRIntl;
var koKRIntl = createIntl('ko_KR', _ko_KR.default);
exports.koKRIntl = koKRIntl;
var idIDIntl = createIntl('id_ID', _id_ID.default);
exports.idIDIntl = idIDIntl;
var deDEIntl = createIntl('de_DE', _de_DE.default);
exports.deDEIntl = deDEIntl;
var faIRIntl = createIntl('fa_IR', _fa_IR.default);
exports.faIRIntl = faIRIntl;
var trTRIntl = createIntl('tr_TR', _tr_TR.default);
exports.trTRIntl = trTRIntl;
var plPLIntl = createIntl('pl_PL', _pl_PL.default);
exports.plPLIntl = plPLIntl;
var hrHRIntl = createIntl('hr_', _hr_HR.default);
exports.hrHRIntl = hrHRIntl;
var intlMap = {
  'mn-MN': mnMNIntl,
  'ar-EG': arEGIntl,
  'zh-CN': zhCNIntl,
  'en-US': enUSIntl,
  'en-GB': enGBIntl,
  'vi-VN': viVNIntl,
  'it-IT': itITIntl,
  'ja-JP': jaJPIntl,
  'es-ES': esESIntl,
  'ca-ES': caESIntl,
  'ru-RU': ruRUIntl,
  'sr-RS': srRSIntl,
  'ms-MY': msMYIntl,
  'zh-TW': zhTWIntl,
  'fr-FR': frFRIntl,
  'pt-BR': ptBRIntl,
  'ko-KR': koKRIntl,
  'id-ID': idIDIntl,
  'de-DE': deDEIntl,
  'fa-IR': faIRIntl,
  'tr-TR': trTRIntl,
  'pl-PL': plPLIntl,
  'hr-HR': hrHRIntl
};
exports.intlMap = intlMap;
var intlMapKeys = Object.keys(intlMap);
exports.intlMapKeys = intlMapKeys;

var ConfigContext = /*#__PURE__*/_react.default.createContext({
  intl: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, zhCNIntl), {}, {
    locale: 'default'
  }),
  valueTypeMap: {}
});

var ConfigConsumer = ConfigContext.Consumer,
    ConfigProvider = ConfigContext.Provider;
/**
 * 根据 antd 的 key 来找到的 locale 插件的 key
 *
 * @param localeKey
 */

exports.ConfigProvider = ConfigProvider;
exports.ConfigConsumer = ConfigConsumer;

var findIntlKeyByAntdLocaleKey = function findIntlKeyByAntdLocaleKey(localeKey) {
  if (!localeKey) {
    return 'zh-CN';
  }

  var localeName = localeKey.toLocaleLowerCase();
  return intlMapKeys.find(function (intlKey) {
    var LowerCaseKey = intlKey.toLocaleLowerCase();
    return LowerCaseKey.includes(localeName);
  });
};
/**
 * 组件解除挂载后清空一下 cache
 *
 * @returns
 */


var CacheClean = function CacheClean() {
  var _useSWRConfig = (0, _swr.useSWRConfig)(),
      cache = _useSWRConfig.cache;

  (0, _react.useEffect)(function () {
    return function () {
      // is a map
      // @ts-ignore
      cache.clear();
    }; // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  return null;
};
/**
 * 如果没有配置 locale，这里组件会根据 antd 的 key 来自动选择
 *
 * @param param0
 */


var ConfigProviderWrap = function ConfigProviderWrap(_ref) {
  var _children = _ref.children,
      _ref$autoClearCache = _ref.autoClearCache,
      autoClearCache = _ref$autoClearCache === void 0 ? false : _ref$autoClearCache;

  var _useContext = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext),
      locale = _useContext.locale; // 如果 locale 不存在自动注入的 AntdConfigProvider


  var Provider = locale === undefined ? _antdV.ConfigProvider : _react.default.Fragment;
  var configProviderDom = (0, _jsxRuntime.jsx)(ConfigConsumer, {
    children: function children(value) {
      var _value$intl;

      var localeName = locale === null || locale === void 0 ? void 0 : locale.locale;
      var key = findIntlKeyByAntdLocaleKey(localeName); // antd 的 key 存在的时候以 antd 的为主

      var intl = localeName && ((_value$intl = value.intl) === null || _value$intl === void 0 ? void 0 : _value$intl.locale) === 'default' ? intlMap[key] : value.intl || intlMap[key]; // 自动注入 antd 的配置

      var configProvider = locale === undefined ? {
        locale: _zh_CN.default
      } : {};
      return (0, _jsxRuntime.jsx)(Provider, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, configProvider), {}, {
        children: (0, _jsxRuntime.jsx)(ConfigProvider, {
          value: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, value), {}, {
            intl: intl || zhCNIntl
          }),
          children: (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
            children: [autoClearCache && (0, _jsxRuntime.jsx)(CacheClean, {}), _children]
          })
        })
      }));
    }
  });
  if (!autoClearCache) return configProviderDom;
  return (0, _jsxRuntime.jsx)(_swr.SWRConfig, {
    value: {
      provider: function provider() {
        return new Map();
      }
    },
    children: configProviderDom
  });
};

exports.ConfigProviderWrap = ConfigProviderWrap;

function useIntl() {
  var _useContext2 = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext),
      locale = _useContext2.locale;

  var _useContext3 = (0, _react.useContext)(ConfigContext),
      intl = _useContext3.intl;

  if (intl && intl.locale !== 'default') {
    return intl;
  }

  if (locale === null || locale === void 0 ? void 0 : locale.locale) {
    return intlMap[findIntlKeyByAntdLocaleKey(locale.locale)];
  }

  return zhCNIntl;
}

var ProProvider = ConfigContext;
exports.ProProvider = ProProvider;
var _default = ConfigContext;
exports.default = _default;